//
//  LLLabelListVC.m
//  LLLabel
//
//  Created by LQ on 2020/12/9.
//

#import "WKLabelListVC.h"
#import "WKLabelConst.h"
@interface WKLabelListVC ()

@end

@implementation WKLabelListVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.viewModel = [WKLabelListVM new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshList) name:WK_NOTIFY_LABELLIST_REFRESH object:nil];
}

- (NSString *)langTitle {
    return LLang(@"标签");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WK_NOTIFY_LABELLIST_REFRESH object:nil];
}

-(void) refreshList {
    [self reloadRemoteData];
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
//    if(indexPath.section<1) {
//        return @[];
//    }
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:LLang(@"删除") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSInteger index = indexPath.row-1;
        WKLabelResp *label = weakSelf.viewModel.labels[index];
        [weakSelf.view showHUD];
        [weakSelf.viewModel requestDeleteLabel:label._id].then(^{
            [weakSelf.view hideHud];
            [weakSelf.viewModel.labels removeObjectAtIndex:index];
            [weakSelf reloadData];
        }).catch(^(NSError *error){
            [weakSelf.view switchHUDError:error.domain];
        });
    }];
    return @[action];
}

@end
